package com.quanshu.exchange.ui.gui.message.chat;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.quanshu.exchange.R;
import com.quanshu.exchange.ui.gui.message.chat.emoji.EmojiconPagerView;
import com.quanshu.exchange.ui.gui.message.chat.expand.ExpandRecyclerView;
import com.quanshu.exchange.ui.gui.message.chat.input.ChatInputView;
import com.quanshu.exchange.ui.gui.message.chat.input.ChatRecorderView;
import com.quanshu.exchange.ui.widget.IndicatorView;
import com.quanshu.exchange.ui.widget.KeyboardStatusDetector;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by jye on 2017/12/11.
 */

public class ChatBottomView extends LinearLayout {

    @BindView(R.id.chat_input_view)
    ChatInputView chatInputView;
    @BindView(R.id.chat_emoji_view)
    EmojiconPagerView chatEmojiView;
    @BindView(R.id.chat_expand_view)
    ExpandRecyclerView chatExpandView;
    @BindView(R.id.chat_expand_container)
    RelativeLayout chatExpandContainer;
    @BindView(R.id.chat_bottom_layout)
    LinearLayout chatBottomLayout;
    @BindView(R.id.chat_bottom_indicator)
    IndicatorView chatBottomIndicator;
    @BindView(R.id.chat_recorder_view)
    ChatRecorderView chatRecorderView;
    private View view;

    private LayoutInflater layoutInflater;

    public void init(FragmentManager fragmentManager, int chatType) {
        chatEmojiView.init(fragmentManager);
        chatExpandView.initExpandMessage(chatType);
    }

    public ChatBottomView(Context context) {
        super(context);
        initView(context);
    }

    public ChatBottomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ChatBottomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ChatBottomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        layoutInflater = LayoutInflater.from(context);
        view = layoutInflater.inflate(R.layout.chat_bottom_view, this);
        ButterKnife.bind(view);
        initListener();
        new KeyboardStatusDetector()
                .registerView(view)
                .setVisibilityListener(keyboardVisible -> {
                    if (keyboardVisible) {
                        hideBottomContainer();
                    }
                });
    }

    private void setShowFlag(int flag) {
        if (flag > -1) {
            chatExpandContainer.setVisibility(View.VISIBLE);
            // show recorder view
            if (flag == 0) {
                chatRecorderView.setVisibility(View.VISIBLE);
                chatEmojiView.setVisibility(View.GONE);
                chatExpandView.setVisibility(View.GONE);
                chatBottomIndicator.setVisibility(View.GONE);
                chatInputView.hideKeyboard();
            }
            // show emoji view
            else if (flag == 1) {

                chatRecorderView.setVisibility(View.GONE);
                chatEmojiView.setVisibility(View.VISIBLE);
                chatExpandView.setVisibility(View.GONE);
                chatBottomIndicator.setVisibility(View.VISIBLE);
                chatInputView.hideKeyboard();
            }
            // show expand view
            else {
                chatRecorderView.setVisibility(View.GONE);
                chatEmojiView.setVisibility(View.GONE);
                chatExpandView.setVisibility(View.VISIBLE);
                chatBottomIndicator.setVisibility(View.VISIBLE);
                chatInputView.hideKeyboard();
            }
        } else {
            chatExpandContainer.setVisibility(View.GONE);
        }
    }

    private void initListener() {
        chatInputView.getExpressionClick().subscribe(o -> {
            setShowFlag(1);
            if (chatEmojiView != null && chatEmojiView.getVisibility() == View.VISIBLE) {
                int i = chatEmojiView.getPageSize();
                chatBottomIndicator.init(i);
            }
        });

        chatEmojiView.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                chatBottomIndicator.selectTo(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        chatInputView.getExtendClick().subscribe(o -> {
            setShowFlag(2);
            if (chatExpandView != null && chatExpandView.getVisibility() == View.VISIBLE) {
                int i = chatExpandView.getPageSize();
                chatBottomIndicator.init(i);
            }
        });
        chatInputView.getVoiceClick().subscribe(o -> setShowFlag(0));
        chatEmojiView.getEmojiClick().subscribe(emojicon -> {
            if (emojicon != null) {
                chatInputView.setInputText(emojicon.getCharX());
            }
        });
        chatEmojiView.getEmojiDeleteClick().subscribe(emojicon -> {
            if (!TextUtils.isEmpty(chatInputView.getInputText().getText().toString())) {
                KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
                chatInputView.getInputText().dispatchKeyEvent(event);
            }
        });
    }

    public BehaviorSubject<String> getSendClick() {
        return chatInputView.getSendClick();
    }

    public void hideBottomContainer() {
        chatExpandContainer.setVisibility(View.GONE);
        chatBottomIndicator.setVisibility(View.GONE);
    }

}

